//
//  DRRegisterViewController.m
//  DoctorRen
//
//  Created by Macx on 2018/11/18.
//

#import "DRRegisterViewController.h"
#import "DRLoginDataModel.h"

@interface DRRegisterViewController ()
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *showPasswordBtn;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSInteger timeCount;

@end

@implementation DRRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.registerBtn.layer.cornerRadius = 22;
    self.timeCount = 60;
    switch (self.type) {
        case DRRegisterVC:
            [self.registerBtn setTitle:@"注册" forState:(UIControlStateNormal)];
            break;
        case DRBindPhoneVC:
            [self.registerBtn setTitle:@"确定" forState:(UIControlStateNormal)];
            break;
        case DRForgetVC:
            [self.registerBtn setTitle:@"确认修改" forState:(UIControlStateNormal)];
            break;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.bottomView setHidden:self.type != DRRegisterVC];
    [self.navigationController setNavigationBarHidden:false animated:animated];
}



- (IBAction)getCodeAction:(id)sender {
    if ([NSString isNullOrEmpty:self.phoneTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"电话不能为空"];
        return;
    }
    [DRLoginDataModel sendMsgCodeWithPhone:self.phoneTF.text Success:^{
        [SVProgressHUD showSuccessWithStatus:@"发送成功"];
        [self startTimer];
    } Fail:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

-(void) startTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeCodeButton) userInfo:nil repeats:true];
    [self changeCodeButton];
}

-(void) changeCodeButton{
    if (self.timeCount > 0) {
        [self.getCodeBtn setEnabled:NO];
        self.getCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%ld秒后重新获取",(long)self.timeCount];
        [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%ld秒后重新获取",self.timeCount] forState:(UIControlStateNormal)];
        [self.getCodeBtn setTitleColor:Text_Gray forState:UIControlStateNormal];
        self.timeCount -= 1;
    } else {
        [self.getCodeBtn setEnabled:YES];
        [self.getCodeBtn setTitle:@"获取验证码" forState:(UIControlStateNormal)];
        [self.getCodeBtn setTitleColor:There_Green forState:UIControlStateNormal];
        self.timeCount = 60;
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (IBAction)showPasswordAction:(id)sender {
    [self.showPasswordBtn setSelected:!self.showPasswordBtn.isSelected];
    [self.passwordTF setSecureTextEntry:!self.showPasswordBtn.isSelected];
}

- (IBAction)agreeAction:(id)sender {
    [self.agreeBtn setSelected:!self.agreeBtn.isSelected];
    
}

- (IBAction)serveiceAgreementAction:(id)sender {
    [self pushWebViewWithUrl:@"http://rsjkkj.com/dist/#/Agreement"];
}

- (IBAction)registerAction:(id)sender {
    if ([NSString isNullOrEmpty:self.phoneTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"电话不能为空"];
        return;
    }
    if ([NSString isNullOrEmpty:self.passwordTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"密码不能为空"];
        return;
    }
    
    if ([NSString isNullOrEmpty:self.codeTF.text]) {
        [SVProgressHUD showErrorWithStatus:@"验证码不能为空"];
        return;
    }
    
    if (!self.agreeBtn.isSelected && self.type == DRRegisterVC) {
        [SVProgressHUD showErrorWithStatus:@"请阅读协议"];
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍等"];
    if (self.type == DRRegisterVC) {
        [DRLoginDataModel registerWithPhone:self.phoneTF.text Password:self.passwordTF.text MsgCode:self.codeTF.text Success:^{
            [self.navigationController popViewControllerAnimated:YES];
            [SVProgressHUD dismiss];
        } Fail:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }];
    }else if (self.type == DRForgetVC){
        [DRLoginDataModel forgetPasswordWithPhone:self.phoneTF.text Password:self.passwordTF.text MsgCode:self.codeTF.text Success:^{
            [self.navigationController popViewControllerAnimated:YES];
            [SVProgressHUD dismiss];
        } Fail:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }];
    }else{
        [DRLoginDataModel bindPhoneWithPhone:self.phoneTF.text Type:self.bindType Uid:self.uid Password:self.passwordTF.text Code:self.codeTF.text Success:^(DRUserModel * _Nonnull user) {
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
            [DRLoginDataModel setIsLogin:YES];
            [self.passwordTF resignFirstResponder];
            [self.codeTF resignFirstResponder];
            [self.passwordTF resignFirstResponder];
            [[NSNotificationCenter defaultCenter] postNotificationName:kLoginSuccess object:nil];
            if ([self.navigationController isEqual:[UIApplication sharedApplication].keyWindow.rootViewController]) {
                UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                KEY_WINDOW.rootViewController = mainSB.instantiateInitialViewController;
                [KEY_WINDOW makeKeyAndVisible];
            }else{
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }

        } Fail:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }];
    }
    
    
}

@end
